home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Halma 1.1.source Folder / Halma ƒ / Halma code ƒ / halma snow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-25  |  5.7 KB  |  233 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        halma snow.c
  4.  
  5. Purpose:    This module handles making it snow while you play Halma.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program in a file named "GNU General Public License".
  19. If not, write to the Free Software Foundation, 675 Mass Ave,
  20. Cambridge, MA 02139, USA.
  21.  
  22. \**********************************************************************/
  23.  
  24. #include "halma snow.h"
  25. #include "program globals.h"
  26.  
  27. #define SNOW_WIDTH 125
  28. #define SNOW_HEIGHT (gNumRows*30-35)
  29. #define SNOW_TOP 10
  30. #define SNOW_LEFT (gNumColumns*30+20)
  31.  
  32. static    Point            gSnow={-2,-2};
  33. static    short            gSnowIter=5;
  34. static    enum snow_types    *gSnowArray=0L;
  35. static    Boolean            gCanSnow=FALSE;
  36. static    short            gBigIter;
  37.  
  38. unsigned char    gSnowSpeed;
  39. unsigned char    gSnowColor;
  40. unsigned char    gSnowMutates;
  41.  
  42. void InitTheSnow(void)
  43. {
  44.     short            i;
  45.     
  46.     gSnowArray=(enum snow_types*)NewPtrClear((SNOW_HEIGHT+2)*SNOW_WIDTH);
  47.     if (gSnowArray==0L)
  48.     {
  49.         gCanSnow=FALSE;
  50.         return;
  51.     }
  52.     gCanSnow=TRUE;
  53.     
  54.     for (i=0; i<SNOW_WIDTH; i++)
  55.         gSnowArray[((SNOW_HEIGHT+1)*SNOW_WIDTH)+i]=gSnowColor;
  56.     
  57.     ResetSnow();
  58. }
  59.  
  60. void ShutDownTheSnow(void)
  61. {
  62.     if (gSnowArray!=0L)
  63.         DisposePtr((Ptr)gSnowArray);
  64.     gSnowArray=0L;
  65. }
  66.  
  67. void SetSnowIter(void)
  68. {
  69.     gBigIter=0;
  70.     gSnowIter=5;
  71. }
  72.  
  73. void SnowIdle(WindowDataHandle theData)
  74. {
  75.     short            r;
  76.     Boolean            notDoneYet;
  77.     
  78.     if (!gCanSnow)
  79.         return;
  80.     
  81.     if (gSnowIter>0)    /* kludge dujour */
  82.     {
  83.         gSnowIter--;
  84.         return;
  85.     }
  86.     
  87.     if ((gSnow.h<0) && (gSnow.v<0))
  88.     {
  89.         gSnow.h=SNOW_LEFT+(Random()&0x7fff)%SNOW_WIDTH;
  90.         gSnow.v=SNOW_TOP;
  91.         if (gSnowSpeed==0x01)
  92.         {
  93.             gBigIter--;
  94.             DrawSnowflakeToScreen(theData, gSnow.h, gSnow.v, snow_white);
  95.         }
  96.         if (SnowIsTouching(gSnow.h, gSnow.v)!=snow_black)
  97.             NewSnow(theData);
  98.     }
  99.     
  100.     notDoneYet=TRUE;
  101.     while (notDoneYet)
  102.     {
  103.         if (gSnowSpeed==0x01)
  104.         {
  105.             gBigIter--;
  106.             DrawSnowflakeToScreen(theData, gSnow.h, gSnow.v, snow_black);
  107.         }
  108.         r=(Random()&0x7fff)%3;
  109.         switch (r)
  110.         {
  111.             case 0:    gSnow.v++;    break;
  112.             case 1:    gSnow.h--;    break;
  113.             case 2:    gSnow.h++;    break;
  114.         }
  115.         if (gSnow.h<SNOW_LEFT)
  116.             gSnow.h+=SNOW_WIDTH;
  117.         if (gSnow.h>=SNOW_LEFT+SNOW_WIDTH)
  118.             gSnow.h-=SNOW_WIDTH;
  119.         notDoneYet=(gSnowSpeed==0x03) ? (SnowIsTouching(gSnow.h, gSnow.v)==snow_black) : FALSE;
  120.         if (gSnowSpeed==0x01)
  121.         {
  122.             gBigIter--;
  123.             DrawSnowflakeToScreen(theData, gSnow.h, gSnow.v, snow_white);
  124.         }
  125.     }
  126.     if (SnowIsTouching(gSnow.h, gSnow.v)!=snow_black)
  127.     {
  128.         DrawSnowflakeToScreen(theData, gSnow.h, gSnow.v, SnowIsTouching(gSnow.h, gSnow.v));
  129.         gSnow.h=gSnow.v=-1;
  130.     }
  131. }
  132.  
  133. void DrawSnowflakeToScreen(WindowDataHandle theData, unsigned short thisX,
  134.     unsigned short thisY, enum snow_types c)
  135. {
  136.     unsigned char    r;
  137.     short            theColor;
  138.     
  139.     if (++gBigIter==50)
  140.     {
  141.         r=((**theData).windowDepth>2) ? (gSnowMutates) ? snow_red+((Random()&0x7fff)%7) :
  142.             gSnowColor : snow_white;
  143.         gBigIter=0;
  144.     }
  145.     else
  146.     {
  147.         r=c;
  148.     }
  149.     switch (r)
  150.     {
  151.         case snow_black:    theColor=blackColor;    break;
  152.         case snow_red:        theColor=redColor;        break;
  153.         case snow_green:    theColor=greenColor;    break;
  154.         case snow_blue:        theColor=blueColor;        break;
  155.         case snow_cyan:        theColor=cyanColor;        break;
  156.         case snow_magenta:    theColor=magentaColor;    break;
  157.         case snow_yellow:    theColor=yellowColor;    break;
  158.         case snow_white:    theColor=whiteColor;    break;
  159.     }
  160.     
  161.     if ((**theData).windowDepth<=2)
  162.         theColor=whiteColor;
  163.     
  164.     SetPortToOffscreen(theData);
  165.     PenMode(patCopy);
  166.     ForeColor(theColor);
  167.     MoveTo(thisX, thisY);
  168.     Line(0,0);
  169.     ForeColor(blackColor);
  170.     RestorePortToScreen(theData);
  171.     PenMode(patCopy);
  172.     ForeColor(theColor);
  173.     MoveTo(thisX, thisY);
  174.     Line(0,0);
  175.     ForeColor(blackColor);
  176.     (**theData).offscreenNeedsUpdate=TRUE;
  177.     gSnowArray[(SNOW_WIDTH*(thisY-SNOW_TOP+1))+thisX-SNOW_LEFT]=r;
  178. }
  179.  
  180. enum snow_types SnowIsTouching(unsigned short thisX, unsigned short thisY)
  181. {
  182.     if ((thisY>SNOW_TOP) && (GetTheSnowPixel(thisX-1,thisY-1)!=snow_black))
  183.         return (GetTheSnowPixel(thisX-1,thisY-1));
  184.     else if ((thisY>SNOW_TOP) && (GetTheSnowPixel(thisX, thisY-1)!=snow_black))
  185.         return (GetTheSnowPixel(thisX, thisY-1));
  186.     else if ((thisY>SNOW_TOP) && (GetTheSnowPixel(thisX+1, thisY-1)!=snow_black))
  187.         return (GetTheSnowPixel(thisX+1, thisY-1));
  188.     else if (GetTheSnowPixel(thisX-1, thisY)!=snow_black)
  189.         return (GetTheSnowPixel(thisX-1, thisY));
  190.     else if (GetTheSnowPixel(thisX+1, thisY)!=snow_black)
  191.         return (GetTheSnowPixel(thisX+1, thisY));
  192.     else if (GetTheSnowPixel(thisX-1, thisY+1)!=snow_black)
  193.         return (GetTheSnowPixel(thisX-1, thisY+1));
  194.     else if (GetTheSnowPixel(thisX, thisY+1)!=snow_black)
  195.         return (GetTheSnowPixel(thisX, thisY+1));
  196.     else if (GetTheSnowPixel(thisX+1, thisY+1)!=snow_black)
  197.         return (GetTheSnowPixel(thisX+1, thisY+1));
  198.     else
  199.         return snow_black;
  200. }
  201.  
  202. enum snow_types GetTheSnowPixel(unsigned short thisX, unsigned short thisY)
  203. {
  204.     return gSnowArray[(SNOW_WIDTH*(thisY-SNOW_TOP+1))+thisX-SNOW_LEFT];
  205. }
  206.  
  207. void NewSnow(WindowDataHandle theData)
  208. {
  209.     Rect            tempRect;
  210.     
  211.     SetRect(&tempRect, SNOW_LEFT, SNOW_TOP, SNOW_LEFT+SNOW_WIDTH, SNOW_TOP+SNOW_HEIGHT);
  212.     SetPortToOffscreen(theData);
  213.     FillRect(&tempRect, black);
  214.     RestorePortToScreen(theData);
  215.     FillRect(&tempRect, black);
  216.     ShutDownTheSnow();
  217.     InitTheSnow();
  218. }
  219.  
  220. void ResetSnow(void)
  221. {
  222.     gSnow.h=-2;
  223.     gSnow.v=-2;
  224.     SetSnowIter();
  225. }
  226.  
  227. void GetRidOfSnowflake(WindowDataHandle theData)
  228. {
  229.     if ((gSnow.h>0) && (gSnow.v>0))
  230.         DrawSnowflakeToScreen(theData, gSnow.h, gSnow.v, snow_black);
  231.     ResetSnow();
  232. }
  233.